﻿using System;
using System.Collections.Generic;
using System.Web.UI;

namespace StupidGrid.Example
{
    public partial class SetUserControlsExample : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            btn_view.Click += btn_view_Click;

            if (!IsPostBack)
            {
                // sometimes you may want to prepopulate the grid with some data
                // do it in three easy steps:

                // 1. create a collection of UserControls
                var someUserControls = new List<UserControl>();

                // 2. put a bunch of user controls in that list
                //      you can use the UserControlPath of StupidGrid to reduce repetition
                for (int i = 0; i < 3; i++)
                {
                    var userControl = (address) LoadControl(stupidgrid_address.UserControlPath);
                    userControl.Address = "Address " + i;
                    userControl.City = "City " + i;
                    userControl.State = "State " + i;
                    someUserControls.Add(userControl);
                }

                // 3. call SetUserControls and send it your collection of UserControls
                stupidgrid_address.SetUserControls(someUserControls);
            }
        }

        void btn_view_Click(object sender, EventArgs e)
        {
            // use RetrieveUserControls to get a list of all the user controls that the user
            // has created up until this point
            //
            // you must specify the usercontrol type between the angle brackets
            // i'm not a fan of this, but I haven't thought of a better way yet
            var allAddresses = stupidgrid_address.RetrieveUserControls<address>();

            lit_results.Text = "";
            foreach (var address in allAddresses)
            {
                lit_results.Text += "<p>Address: " + address.Address + ", " + address.City + ", " + address.State + "</p>";
            }
        }
    
    }
}
